package com.celan.year2023.month02.day09;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class Solution {
    // 3 + 5/2 + 5/2
    public int calculate(String s) {
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        int num = 0;
        char pre = '+';
        int n = s.length();
        for (int i = 0; i < n; i++) {
            //如果为数字
            if (Character.isDigit(s.charAt(i))) {
                num = num * 10 + (s.charAt(i) - '0');
            }
            //如果为符号
            if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || i == n - 1) {
                switch (pre) {
                    case '+':
                        stack.push(num);
                        break;
                    case '-':
                        stack.push(-num);
                        break;
                    case '*':
                        stack.push(stack.pop() * num);
                        break;
                    default:
                        stack.push(stack.pop() / num);
                }
                num = 0;
                pre = s.charAt(i);
            }
        }
        while (!stack.isEmpty()) res += stack.pop();
        return res;
    }

    public void deleteNode(ListNode node) {
        //从后往前的值一直覆盖
        ListNode p = node;
        ListNode pre = null;
        while (p.next != null) {
            p.val = p.next.val;
            pre = p;
            p = p.next;
        }
        pre.next = null;
    }
}
